package com.bw.zhuangao5___day002.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.zhuangao5___day002.Activity.Janewyer.Janewyer_WoredErweimaActivity;
import com.bw.zhuangao5___day002.Activity.Janewyer.Janewyer_WoredSimiActivity;
import com.bw.zhuangao5___day002.Adpter.FragmentAdpter.FragmentAdpterTwo;
import com.bw.zhuangao5___day002.R;
import com.bw.zhuangao5___day002.UI.Login.BlankFragmentLogin;
import com.bw.zhuangao5___day002.UI.Reister.BlankFragmentReister;
import com.frame.lib_common.slidingmenu.SlidingMenu;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class JanewyerActivity extends AppCompatActivity {
    /**
     * 该方法用来捕捉手机键盘被按下的事件。
     * @param keyCode  该参数指的是被按下的键的键盘码，手机键盘中每个按钮都会有其对应的键盘码，
     *                 在应用程序都是通过键盘码才知道用户按下的是哪个键。
     * @param event    当用户按下按键时，系统会自动将事件封装成KeyEvent对象供应用程序使用。
     *                 按键事件KeyEvent 按键事件有多种常量类型，比如 KEYCODE_BACK
     * @return
     */
    Long exitTime =0L;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ){
            //判断用户两次按键的时间差是否在一个预期值之内，是的话直接直接退出，不是的话提示用户再按一次后退键退出。
            if(System.currentTimeMillis() - exitTime > 2000){
                Toast.makeText(this,"在点就退出",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                //当返回true时，表示已经完整地处理了这个事件，并不希望其他的回调方法再次进行处理，而当返回false时，
                // 表示并没有完全处理完该事件，更希望其他回调方法继续对其进行处理，
                return true;
            }else{
                finish(); //结束当前activity
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public static ViewPager YangChenActivityViewPager;
    public TabLayout YangChenActivityTabLayout;

    public static void day1() {
        YangChenActivityViewPager.setCurrentItem(1);
    }


    public static void day2() {
        YangChenActivityViewPager.setCurrentItem(0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yng_chen);
        initView();
        onWindowFocusChanged(true);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new BlankFragmentLogin());
        fragmentList.add(new BlankFragmentReister());

        List<String> stringList = new ArrayList<>();
        stringList.add("登录");
        stringList.add("注册");

        FragmentAdpterTwo adpterTwo = new FragmentAdpterTwo(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragmentList,stringList);
        YangChenActivityViewPager.setAdapter(adpterTwo);
        YangChenActivityTabLayout.setupWithViewPager(YangChenActivityViewPager);

        SlidingMenu slidingMenu = new SlidingMenu(this);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setBehindWidth(900);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setMenu(R.layout.activity_slidngmenu);
        slidingMenu.showContent();
        View menu = slidingMenu.getMenu();//实现点击事件
        menu.findViewById(R.id.wordErwei).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                        Intent intent = new Intent(JanewyerActivity.this, Janewyer_WoredErweimaActivity.class);
                        startActivity(intent);

            }
        });

        menu.findViewById(R.id.wordSimi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(JanewyerActivity.this);
                builder.setIcon(R.mipmap.shezhi);
                builder.setTitle("私人娱乐空间");
                builder.setMessage("请谨慎");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.showShort("阿阿");
                        PopupWindow popupWindow = new PopupWindow();
                        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                        View inflate = LayoutInflater.from(JanewyerActivity.this).inflate(R.layout.activity_popupwindow, null);
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.setContentView(inflate);
                        popupWindow.setFocusable(true);
                        EditText viewById = inflate.findViewById(R.id.shows);
                        inflate.findViewById(R.id.queryEnd).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                               if (!TextUtils.isEmpty(viewById.getText().toString()))
                               {
                                   if (viewById.getText().toString().equals("yang")){

                                       ToastUtils.showShort("欢迎《主人大大》");
                                       Intent intent = new Intent(JanewyerActivity.this, Janewyer_WoredSimiActivity.class);
                                       startActivity(intent);

                                   }else {
                                       ToastUtils.showShort("你不是《主人》");
                                   }
                               }else {
                                   ToastUtils.showShort("未满18周岁@未成年禁止进入");
                               }

                            }
                        });
                        popupWindow.showAsDropDown(v,Gravity.BOTTOM,1000,1000);


                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.showShort("好好");
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });


    }



    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }


    private void initView() {
        YangChenActivityViewPager = findViewById(R.id.YangChenActivity_viewPager);
        YangChenActivityTabLayout = findViewById(R.id.YangChenActivity_TabLayout);
    }
}
